/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import who.tmap.ui.util.SpringUtilities;

/**
 * @author stl
 *
 * A window to input map geographic dimensions
 */
public class JDialogGeographicBox extends JDialog {

    private double xMin =0;
    private double xMax =0;
    private double yMin =0;
    private double yMax =0;
    private boolean okClicked =false;
    private boolean dataHasBeenUpdated =false;
    
	private JPanel jContentPane = null;

	private JPanel jPanelFields = null;
	private JButton jButtonOK = null;
	private JTextField jTextFieldXMin = null;
	private JTextField jTextFieldXMax = null;
	private JTextField jTextFieldYMin = null;
	private JTextField jTextFieldYMax = null;
	
	private JPanel jPanelButtons = null;
	private JButton jButtonCancel = null;
	private JCheckBox jCheckBoxRescale = null;
	
	/**
	 * This is the default constructor
	 */
	public JDialogGeographicBox( Frame parent ) {
		super( parent );
		initialize();
	}
	
	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setTitle("Geographic box");
		this.setSize(380,200);
		this.setContentPane(getJContentPane());
		this.setLocationRelativeTo( null );
		
		getRootPane().setDefaultButton( getJButtonOK() );
	}
	
	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if ( jContentPane == null ) {
			jContentPane = new JPanel();
			jContentPane.setLayout( new BorderLayout() );//new BoxLayout(jContentPane, BoxLayout.Y_AXIS));
			jContentPane.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));		
			jContentPane.add(getJPanelFields(), BorderLayout.NORTH );
			jContentPane.add(getJPanelButtons(), BorderLayout.SOUTH );
			
		}
		return jContentPane;
	}
	
	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelFields() {
		if (jPanelFields == null) {
			jPanelFields = new JPanel( new SpringLayout() );	    
		    
			JLabel jLabelXMin = new JLabel( "X Min" );
			jPanelFields.add( jLabelXMin );
			jPanelFields.add( getJTextFieldXMin() );
			jLabelXMin.setLabelFor( getJTextFieldXMin() );
			
			JLabel jLabelXMax = new JLabel( "X Max" );			
			jPanelFields.add( jLabelXMax );
			jPanelFields.add( getJTextFieldXMax() );
			jLabelXMax.setLabelFor( getJTextFieldXMax() );		
			
			JLabel jLabelYMin = new JLabel( "Y Min" );			
			jPanelFields.add( jLabelYMin );
			jPanelFields.add( getJTextFieldYMin() );
			jLabelYMin.setLabelFor( getJTextFieldYMin() );
			
			JLabel jLabelYMax = new JLabel( "Y Max (top of the window)" );			
			jPanelFields.add( jLabelYMax );
			jPanelFields.add( getJTextFieldYMax() );
			jLabelYMax.setLabelFor( getJTextFieldYMax() );	
			
			SpringUtilities.makeCompactGrid( jPanelFields,
	                4, 2, //rows, cols
	                5, 5, //initX, initY
	                5, 5);  			

		}
		return jPanelFields;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonOK() {
		if (jButtonOK == null) {
			jButtonOK = new JButton();
			jButtonOK.setText("OK");
			
			jButtonOK.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
				    jButtonOK_actionPerformed();
				}
			});
		}

		return jButtonOK;
	}
	
	private double getDouble( String str ) {
	    double dbl;
	    try {
	        dbl =Double.parseDouble( str );
	    } catch( Exception e ) {
	        dbl =0;
	    }
	    
	    return dbl;
	}
	
	private void jButtonOK_actionPerformed() {
	    okClicked =true;
	    if ( dataHasBeenUpdated ) {
	        setXMin( getDouble( jTextFieldXMin.getText() ) );
	        setXMax( getDouble( jTextFieldXMax.getText() ) );
	        setYMin( getDouble( jTextFieldYMin.getText() ) );
	        setYMax( getDouble( jTextFieldYMax.getText() ) );	        
	    }
	    setVisible( false );
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldXMin() {
		if (jTextFieldXMin == null) {
			jTextFieldXMin = new JTextField();
			jTextFieldXMin.setHorizontalAlignment(JTextField.RIGHT);
			jTextFieldXMin.addKeyListener(new KeyAdapter() { 
				public void keyTyped(KeyEvent e) {    
				    dataHasBeenUpdated =true;
				}
			});

		}
		return jTextFieldXMin;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldXMax() {
		if (jTextFieldXMax == null) {
			jTextFieldXMax = new JTextField();
			jTextFieldXMax.setHorizontalAlignment(JTextField.RIGHT);
			jTextFieldXMax.addKeyListener(new KeyAdapter() { 
				public void keyTyped(KeyEvent e) {    
				    dataHasBeenUpdated =true;
				}
			});
		}
		return jTextFieldXMax;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldYMin() {
		if (jTextFieldYMin == null) {
			jTextFieldYMin = new JTextField();
			jTextFieldYMin.setHorizontalAlignment(JTextField.RIGHT);
			jTextFieldYMin.addKeyListener(new KeyAdapter() { 
				public void keyTyped(KeyEvent e) {    
				    dataHasBeenUpdated =true;
				}
			});
		}
		return jTextFieldYMin;
	}
	
	/**
	 * This method initializes jTextField	
	 * 	
	 * @return javax.swing.JTextField	
	 */    
	private JTextField getJTextFieldYMax() {
		if (jTextFieldYMax == null) {
			jTextFieldYMax = new JTextField();
			jTextFieldYMax.setHorizontalAlignment(JTextField.RIGHT);
			jTextFieldYMax.addKeyListener(new KeyAdapter() { 
				public void keyTyped(KeyEvent e) {    
				    dataHasBeenUpdated =true;
				}
			});
		}
		return jTextFieldYMax;
	}
	
    /**
     * @return Returns the xMax.
     */
    public double getXMax() {
        return xMax;
    }
    
    /**
     * @param max The xMax to set.
     */
    public void setXMax(double max) {
        xMax = max;
        jTextFieldXMax.setText( String.valueOf( xMax ) );        
    }
    
    /**
     * @return Returns the xMin.
     */
    public double getXMin() {
        return xMin;
    }
    
    /**
     * @param min The xMin to set.
     */
    public void setXMin(double min) {
        xMin = min;
        jTextFieldXMin.setText( String.valueOf( xMin ) );        
    }
    
    /**
     * @return Returns the yMax.
     */
    public double getYMax() {
        return yMax;
    }
    
    /**
     * @param max The yMax to set.
     */
    public void setYMax(double max) {
        yMax = max;
        jTextFieldYMax.setText( String.valueOf( yMax ) );        
    }
    
    /**
     * @return Returns the yMin.
     */
    public double getYMin() {
        return yMin;
    }
    
    /**
     * @param min The yMin to set.
     */
    public void setYMin(double min) {
        yMin = min;
        jTextFieldYMin.setText( String.valueOf( yMin ) );        
    }

	/**
	 * This method initializes jPanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */    
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			jPanelButtons = new JPanel();

			jPanelButtons.add( getJButtonOK(), null );
			jPanelButtons.add( getJButtonCancel(), null );
		}
		return jPanelButtons;
	}
	
	/**
	 * This method initializes jButton	
	 * 	
	 * @return javax.swing.JButton	
	 */    
	private JButton getJButtonCancel() {
		if (jButtonCancel == null) {
			jButtonCancel = new JButton();
			jButtonCancel.setText("Cancel");
			jButtonCancel.addActionListener(new ActionListener() { 
				public void actionPerformed(ActionEvent e) {    
				    setVisible( false );
				}
			});
		}
		return jButtonCancel;
	}
	
	public boolean okHasBeenClicked() {
	    return okClicked; 
	}
	
	public boolean dataHasChanged() {
	    return dataHasBeenUpdated;
	}

  }
